package com.xxcxcxcx.jobsystem.common.UI;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.xxcxcxcx.jobsystem.Main;
import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import com.xxcxcxcx.jobsystem.common.component.ImageWidget;
import com.xxcxcxcx.jobsystem.common.component.ProgressBarWidget;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.gui.ScrollPanel;

import java.util.List;


@SuppressWarnings("ALL")
@OnlyIn(Dist.CLIENT)
public class JobUI extends Screen {

    private int WindowTLX = width/2-88;
    private int WindowTLY = height/2-83;
    Button pageNext;
    Button pagePrev;
    Button prevBtn;
    Button nextBtn;
    Button invBtn;



    ScrollPanel scrollPanel;
    ProgressBarWidget progressBar;
    ImageWidget jobIcon;
    ResourceLocation BACKGROUND_LOCATION = new ResourceLocation(Main.MOD_ID,"textures/gui/skillpage.png");
    ResourceLocation JOB_ICONS_LOCATION = new ResourceLocation(Main.MOD_ID,"textures/job_icons.png");

    int level;
    double exp;
    double coeff;

    int descriptionline = 0;
    int maxline = 0;
    int pageline = 11;

    int jobID = 0;
    int jobIDCount = JobsClass.Jobs.length;

    public JobUI(Component title) {
        super(title);
    }
    @Override
    public void init()
    {
        //textSlider = new ForgeSlider(this.width/2+88,this.height/2-34,8,110,null,null,0,1,0,0.001,0,false);
        prevBtn = new Button(this.width/2 - 80 ,this.height/2 - 67,16,16,new TranslatableComponent("jobsystem.ui.prev"),(button)->{
            this.jobID = (jobID - 1 + jobIDCount) % jobIDCount;
            descriptionline = 0;
        });
        this.addWidget(prevBtn);
        nextBtn = new Button(this.width/2 + 64 ,this.height/2 - 67,16,16,new TranslatableComponent("jobsystem.ui.next"),(button)->{
            this.jobID = (jobID + 1) % jobIDCount;
            descriptionline = 0;
        });
        pageNext = new Button(this.width/2 + 64 ,this.height/2 + 59,16,16,new TranslatableComponent("jobsystem.ui.next"),(button)->{
            if(descriptionline+pageline<maxline)
                descriptionline += pageline;
        });
        pagePrev = new Button(this.width/2 - 80 ,this.height/2 + 59,16,16,new TranslatableComponent("jobsystem.ui.prev"),(button)->{
            descriptionline -= pageline;
            if(descriptionline<0)descriptionline=0;
        });

        invBtn = new Button(this.width/2-104,this.height/2-83,16,16, new TextComponent("J"),(button)->{
            Minecraft.getInstance().setScreen(new InventoryScreen(Minecraft.getInstance().player));
        });
        this.addWidget(invBtn);
        this.addWidget(nextBtn);
        this.addWidget(pageNext);
        this.addWidget(pagePrev);
        progressBar = new ProgressBarWidget(this.width/2 - 80, this.height/2 - 42,160, 5, new TextComponent("Exp Progress"));
        this.addWidget(progressBar);
    }
    @Override
    public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTicks)
    {
        renderBackground(poseStack);
        RenderSystem.setShaderColor(1.0f,1.0f,1.0f,1.0f);
        RenderSystem.setShaderTexture(0,BACKGROUND_LOCATION);
        blit(poseStack, this.width/2 - 88, this.height/2 -83,0,0,176,166);
        //绘制职业图标
        RenderSystem.setShaderTexture(0,JOB_ICONS_LOCATION);
        blit(poseStack, this.width/2 - 56, this.height/2 - 75, (jobID%4)*32, (jobID/4)*32, 32, 32);

        prevBtn.render(poseStack,mouseX,mouseY,partialTicks);

        nextBtn.render(poseStack,mouseX,mouseY,partialTicks);

        invBtn.render(poseStack,mouseX,mouseY,partialTicks);

        pageNext.render(poseStack,mouseX,mouseY,partialTicks);
        pagePrev.render(poseStack,mouseX,mouseY,partialTicks);
        LocalPlayer currentPlayer;

        currentPlayer = Minecraft.getInstance().player;

        getPlayerInfo(JobsClass.Jobs[jobID], JobCapabilityProvider.getPlayerData(currentPlayer).orElse(new JobCapability(currentPlayer)));

        drawJobName(poseStack);
        drawJobDescription(poseStack);
        //textSlider.render(poseStack,mouseX,mouseY,partialTicks);

        if(level<10 && level>=0)
        {
            progressBar.updatePercentage((float) exp/JobsClass.xp[level]);
            TextComponent percentage = new TextComponent((float) (exp / JobsClass.xp[level]) * 100 +"%");
            drawCenteredString(poseStack,font,percentage,width/2,height/2-43,0x00ffff);
        }
        else
        {
            progressBar.updatePercentage(1);
            drawCenteredString(poseStack,font,"∞",width/2,height/2-42,0x00ffff);
        }

        progressBar.render(poseStack,mouseX,mouseY,partialTicks);
        drawCenteredString(poseStack,font,new TranslatableComponent("jobsystem.ui.page",descriptionline/pageline +1,(maxline-1)/pageline +1 ),width/2,height/2+63,0xffffff);

    }

    private void getPlayerInfo(String job, PlayerSaveData player )
    {
        level = player.getLevel(job);//获取玩家相关技能等级
        exp = player.getExperience(job);//获取玩家相关技能等级
        coeff =  player.getCoefficient(job);//获取玩家coeff
    }

    //绘制职业名
    private void drawJobName(PoseStack poseStack) {
        TextComponent jobName = new TextComponent((new TranslatableComponent("jobsystem.job."+JobsClass.Jobs[jobID])).getString());
        drawString(poseStack,font,jobName,this.width/2 - 15,this.height/2 - 74,0xffffff);
        TextComponent jobLevel = new TextComponent(new TranslatableComponent("jobsystem.ui.level", Integer.toString(level)).getString());
        drawString(poseStack,font,jobLevel,this.width/2 - 15,this.height/2 - 66,0xffffff);
        TextComponent jobCoeff = new TextComponent(new TranslatableComponent("jobsystem.ui.coefficient", coeff).getString());
        drawString(poseStack,font,jobCoeff,this.width/2 - 15,this.height/2 - 58,0xffffff);
    }
    //绘制职业描述文档
    private void drawJobDescription(PoseStack poseStack) {
        TextComponent jobDescription = new TextComponent(new TranslatableComponent("jobsystem.ui."+JobsClass.Jobs[jobID]+".description").getString());
        //MultiLineLabel mll =MultiLineLabel.create(font,jobDescription,160);//.renderLeftAligned(poseStack,this.width/2 - 79,this.height/2 - 34,8,0xffffff)
        List<FormattedText> lines= font.getSplitter().splitLines(jobDescription.getString(),158,Style.EMPTY);
        maxline = lines.size();
        System.out.println(maxline);
        StringBuilder displayLines = new StringBuilder();
        for(int i =descriptionline; i<descriptionline+pageline && i<lines.size();i++)
        {
            displayLines.append(lines.get(i).getString()).append("\n");
        }
        MultiLineLabel.create(font, FormattedText.of(displayLines.toString()),158).renderLeftAligned(poseStack,this.width/2 - 79,this.height/2 - 34,8,0xffffff);
    }

    @Override
    public boolean isPauseScreen()
    {
        return false;
    }

}
